package org.lan.service.impl;

import org.lan.entity.Group;
import org.lan.entity.Resource;
import org.lan.entity.User;
import org.lan.repository.GroupResourceRepository;
import org.lan.repository.UserGroupRepository;
import org.lan.repository.UserRepository;
import org.lan.service.IUserService;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class UserService implements IUserService {

    private UserRepository userRepository;

    private UserGroupRepository userGroupRepository;

    private GroupResourceRepository groupResourceRepository;

    @Inject
    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Inject
    public void setUserGroupRepository(UserGroupRepository userGroupRepository) {
        this.userGroupRepository = userGroupRepository;
    }

    @Inject
    public void setGroupResourceRepository(GroupResourceRepository groupResourceRepository) {
        this.groupResourceRepository = groupResourceRepository;
    }

    @Override
    public User findUserByUsername(String username) {
        return userRepository.findUserByUsername(username);
    }

    @Override
    public List<Group> findUserGroupGroupByUserId(Integer id) {
        return userGroupRepository.findUserGroupGroupByUserId(id);
    }

    @Override
    public Set<String> findResourcePermissionString(Integer userId) {
        Set<String> permissionString = new HashSet<String>();
        List<Group> groups = userGroupRepository.findUserGroupGroupByUserId(userId);
        groups.forEach((group -> {
            int groupId = group.getId();
            List<Resource> resources = groupResourceRepository.findResourceByGroupId(groupId);
            resources.forEach(resource -> {
                String url = resource.getUrl();
                permissionString.add(url);
            });
        }));
        return permissionString;
    }


}
